package com.queen.testretrofitrxjava.ui.fragments;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.queen.testretrofitrxjava.R;
import com.queen.testretrofitrxjava.applications.ApplicationController;
import com.queen.testretrofitrxjava.entity.MovieDetail;
import com.queen.testretrofitrxjava.entity.Subject;
import com.queen.testretrofitrxjava.internal.di.components.DaggerTopMovieComponent;
import com.queen.testretrofitrxjava.presenter.MovieDetailPresenter;
import com.queen.testretrofitrxjava.presenter.TopMoviePresenter;
import com.queen.testretrofitrxjava.presenter.presenterView.BasePresenterView;
import com.queen.testretrofitrxjava.utils.StringUtil;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by liukun on 16/3/19.
 */
public class MovieDetailFragment extends Fragment implements BasePresenterView<MovieDetail>{

    public static final String TAG = MovieDetailFragment.class.getSimpleName();

    private static final String KEY_MOVIE_ID = "movie_id";

    @Bind(R.id.movie_detail_SDV)
    SimpleDraweeView movieDetailSDV;
    @Bind(R.id.movie_detail_title_TV)
    TextView movieDetailTitleTV;
    @Bind(R.id.movie_detail_director_TV)
    TextView movieDetailDirectorTV;
    @Bind(R.id.movie_detail_cast_TV)
    TextView movieDetailCastTV;
    @Bind(R.id.movie_detail_original_TV)
    TextView movieDetailOriginalTV;
    @Bind(R.id.movie_detail_year_TV)
    TextView movieDetailYearTV;
    @Bind(R.id.movie_detail_genres_TV)
    TextView movieDetailGenresTV;
    @Bind(R.id.movie_detail_countries_TV)
    TextView movieDetailCountriesTV;
    @Bind(R.id.movie_detail_summary_TV)
    TextView movieDetailSummaryTV;
    @Bind(R.id.fragment_movie_detail_SV)
    ScrollView fragmentMovieDetailSV;

    private String movieId;

    @Inject
    MovieDetailPresenter movieDetailPresenter;

    public static MovieDetailFragment getInstance(String movieId){
        Bundle b = new Bundle();
        b.putString(KEY_MOVIE_ID, movieId);
        MovieDetailFragment instance = new MovieDetailFragment();
        instance.setArguments(b);
        return instance;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.movieId = getArguments().getString(KEY_MOVIE_ID);

        //依赖注入
        DaggerTopMovieComponent.builder()
                .applicationComponent(ApplicationController.getsInstance().getApplicationComponent())
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_movie_detail, container, false);
        ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        movieDetailPresenter.setView(this);

        if (savedInstanceState == null) {
            movieDetailPresenter.requestMovieDetail(movieId);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void renderView(MovieDetail movieDetail) {

        movieDetailSDV.setImageURI(Uri.parse(movieDetail.getImages().getLarge()));

        movieDetailTitleTV.setText(movieDetail.getTitle());

        movieDetailDirectorTV.setText("导演：" + StringUtil.joinSubjectCastNamesWhitSpace(movieDetail.getDirectors()));

        movieDetailCastTV.setText("主演：" + StringUtil.joinSubjectCastNamesWhitSpace(movieDetail.getCasts()));

        movieDetailOriginalTV.setText("原名：" + movieDetail.getOriginal_title());

        movieDetailYearTV.setText("年代：" + movieDetail.getYear());

        movieDetailGenresTV.setText("类型：" + StringUtil.joinItem(movieDetail.getGenres()));

        movieDetailCountriesTV.setText("地区：" + StringUtil.joinItem(movieDetail.getCountries()));

        movieDetailSummaryTV.setText(movieDetail.getSummary());
    }
}
